package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jzo2o
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {
    /**
     * 0
     */
    private static final int ZERO = 0;

    @Resource
    private ServeItemMapper serveItemMapper;
    @Resource
    private RegionMapper regionMapper;

    @Override
    public PageResult<ServeResDTO> pageQuery(ServePageQueryReqDTO servePageQueryReqDTO) {
        return PageHelperUtils
                .selectPage(servePageQueryReqDTO, () -> baseMapper
                        .queryListByReginId(servePageQueryReqDTO.getRegionId()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchAdd(List<ServeUpsertReqDTO> serveUpsertReqDTOList) {
        if (ObjectUtil.isEmpty(serveUpsertReqDTOList)){
            throw new ForbiddenOperationException("新增数据不能为空");
        }
        ArrayList<Serve> serveList = new ArrayList<>(serveUpsertReqDTOList.size());
        for (ServeUpsertReqDTO serveUpsertReqDTO : serveUpsertReqDTOList) {
            //校验服务状态是否是启用状态
            ServeItem serveItem = serveItemMapper.selectById(serveUpsertReqDTO.getServeItemId());
            if (ObjectUtil.isEmpty(serveItem) || serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()){
                throw new ForbiddenOperationException("服务项不存在或未启用");
            }
            //校验是否重复新增
             Integer count =  lambdaQuery()
                    .eq(Serve :: getRegionId , serveUpsertReqDTO.getRegionId())
                    .eq(Serve :: getServeItemId , serveUpsertReqDTO.getServeItemId())
                    .count();
            if (count > ZERO){
                throw new ForbiddenOperationException(serveItem.getName() + "该区域已存在该服务项");
            }
            //业务处理
            Serve serve = BeanUtil.toBean(serveUpsertReqDTO, Serve.class);
            Region region = regionMapper.selectById(serveUpsertReqDTO.getRegionId());
            if (ObjectUtil.isNotEmpty(region)){
                throw new ForbiddenOperationException("该区域不存在");
            }
            serve.setCityCode(region.getCityCode());
            serveList.add(serve);
        }
        //批量新增
        saveBatch(serveList);
    }
}
